import requests
from lxml import etree

# from download_img import download_image


headers = {
    "authority": "cn.bing.com",
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9",
    "origin": "https://www.bing.com",
    "referer": "https://www.bing.com",
    "sec-ch-ua": "\"Google Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"",
    "sec-ch-ua-arch": "\"x86\"",
    "sec-ch-ua-bitness": "\"64\"",
    "sec-ch-ua-full-version": "\"117.0.5938.92\"",
    "sec-ch-ua-full-version-list": "\"Google Chrome\";v=\"117.0.5938.92\", \"Not;A=Brand\";v=\"8.0.0.0\", \"Chromium\";v=\"117.0.5938.92\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-model": "\"\"",
    "sec-ch-ua-platform": "\"Linux\"",
    "sec-ch-ua-platform-version": "\"5.15.0\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
}
cookies = {
    "ipv6": "hit=1697686078057&t=4",
    "MUID": "2911F544E9626B8539BBE6DCE86A6A98",
    "MUIDB": "2911F544E9626B8539BBE6DCE86A6A98",
    "_EDGE_S": "SID=1FE73487CE7A66B91FF12728CF1C67D1",
    "MMCASM": "ID=C2EF44F8D1A149588D5463EC673ECF56",
    "SRCHD": "AF=NOFORM",
    "SRCHUID": "V=2&GUID=66675A7981C845039B91CF0C121AAD44&dmnchg=1",
    "SRCHUSR": "DOB=20231019&T=1697682470000&TPC=1697682472000",
    "_Rwho": "u=d",
    "_RwBf": "r=0&ilt=4&ihpd=0&ispd=0&rc=12&rb=0&gb=0&rg=200&pc=9&mtu=0&rbb=0&g=0&cid=&clo=0&v=4&l=2023-10-18T07:00:00.0000000Z&lft=0001-01-01T00:00:00.0000000&aof=0&o=2&p=&c=&t=0&s=0001-01-01T00:00:00.0000000+00:00&ts=2023-10-19T02:29:18.2703329+00:00&rwred=0&wls=&wlb=&lka=0&lkt=0&aad=0&TH=",
    "_SS": "SID=1FE73487CE7A66B91FF12728CF1C67D1&R=12&RB=0&GB=0&RG=200&RP=9",
    "SRCHHPGUSR": "SRCHLANG=zh-Hans&PV=5.15.0&BRW=HTP&BRH=M&CW=947&CH=860&SCW=1177&SCH=10413&DPR=2.0&UTC=480&DM=0&PRVCW=1212&PRVCH=860&WTS=63833279270&HV=1697682566"
}
url = "https://cn.bing.com/images/async"
def get_img(i,key):
    params = {
        "q": key,
        "first": str(11+ 35*i),
        "count": "35",
        "cw": "1197",
        "ch": "845",
        "relp": "35",
        "apc": "0",
        "datsrc": "I",
        "layout": "RowBased_Landscape",
        "mmasync": "1",
        "dgState": "x*331_y*1512_h*182_c*1_i*176_r*43",
        "IG": "0C3C4505A6AC430F83037F94FC8FE24D",
        "SFX": str(i),
        "iid": "images.5567"
    }
    response = requests.get(url, headers=headers, cookies=cookies)
    html=etree.HTML(response.text)
    url_list=html.xpath("//div[@class='infopt']/a/@href")
    return url_list


def download_img(url):

    url = "https://cn.bing.com/images/search"
    headers = {
    "authority": "cn.bing.com",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "accept-language": "zh-CN,zh;q=0.9",
    "referer": "https://cn.bing.com/images/search?q=%E7%89%9B%E9%A9%AC&qs=n&form=QBIDMH&sp=-1&lq=0&sc=0-0&cvid=883E56AB2C984E329277FD673AFAA8A7&ghsh=0&ghacc=0&first=1",
    "sec-ch-ua": "\"Google Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"",
    "sec-ch-ua-arch": "\"x86\"",
    "sec-ch-ua-bitness": "\"64\"",
    "sec-ch-ua-full-version": "\"117.0.5938.92\"",
    "sec-ch-ua-full-version-list": "\"Google Chrome\";v=\"117.0.5938.92\", \"Not;A=Brand\";v=\"8.0.0.0\", \"Chromium\";v=\"117.0.5938.92\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-model": "\"\"",
    "sec-ch-ua-platform": "\"Linux\"",
    "sec-ch-ua-platform-version": "\"5.15.0\"",
    "sec-fetch-dest": "iframe",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "same-origin",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
}
    cookies = {
    "_IDET": "MIExp=0&VSNoti2=20231019",
    "ipv6": "hit=1697702183421&t=4",
    "MUID": "2911F544E9626B8539BBE6DCE86A6A98",
    "MUIDB": "2911F544E9626B8539BBE6DCE86A6A98",
    "MMCASM": "ID=C2EF44F8D1A149588D5463EC673ECF56",
    "SRCHD": "AF=NOFORM",
    "SRCHUID": "V=2&GUID=66675A7981C845039B91CF0C121AAD44&dmnchg=1",
    "_Rwho": "u=d",
    "_EDGE_S": "SID=1FE73487CE7A66B91FF12728CF1C67D1&ui=zh-cn&mkt=en-us",
    "SRCHUSR": "DOB=20231019&T=1697698572000&TPC=1697682472000",
    "SRCHHPGUSR": "SRCHLANG=zh-Hans&PV=5.15.0&BRW=N&BRH=T&CW=1255&CH=1016&SCW=1240&SCH=2147&DPR=2.0&UTC=480&DM=0&PRVCW=1255&PRVCH=1016&WTS=63833295372&HV=1697700633&IG=883E56AB2C984E329277FD673AFAA8A7",
    "_RwBf": "r=0&ilt=6&ihpd=0&ispd=0&rc=20&rb=0&gb=0&rg=200&pc=17&mtu=0&rbb=0&g=0&cid=&clo=0&v=1&l=2023-10-19T07:00:00.0000000Z&lft=0001-01-01T00:00:00.0000000&aof=0&o=2&p=&c=&t=0&s=0001-01-01T00:00:00.0000000+00:00&ts=2023-10-19T07:30:33.3356705+00:00&rwred=0&wls=&wlb=&lka=0&lkt=0&aad=0&TH=",
    "_SS": "SID=1FE73487CE7A66B91FF12728CF1C67D1&R=20&RB=0&GB=0&RG=200&RP=17"
}
    params = {
    "view": "detailV2",
    "ccid": "JiLIXnj0",
    "id": "4620F004B247093381F57A833B118712DD5FCC25",
    "thid": "OIP.JiLIXnj0LvtqsYARwEoZ2gHaFQ",
    "mediaurl": "https://tgi12.jia.com/129/508/29508294.png",
    "exph": "355",
    "expw": "500",
    "q": "路人",
    "simid": "608009160292063623",
    "FORM": "IRPRST",
    "ck": "C3514DA14DAF3B3F42EB823564BBB315",
    "selectedIndex": "3",
    "mode": "overlay"
}
    response = requests.get(url, headers=headers, cookies=cookies, params=params)
    html=etree.HTML(response.text)
    try:
        url_list=html.xpath('//*[@id="mainImageWindow"]/div/div/div/div/img/@src')[0]
        return url_list
    except:
        pass

url_list=[]
search_key='路人'
for i in range(2,3):
    url_list=url_list+get_img(i,search_key)
img_list=[download_img('https://cn.bing.com/'+url) for url in url_list]
print(img_list)
# [ download_image(search_key,i,v) for i,v in enumerate(img_list)]